﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.DataManager
{
    public partial class ShopManagerControl : BaseUserControl
    {
        DataServiceReference.DataServiceClient dataClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;

        public ShopManagerControl()
        {
            InitializeComponent();
            dataClient = new DataServiceReference.DataServiceClient();
            InitData();
            InitEvent();
            QueryData();
        }

        Tbl_Shop CuTbl_Shop
        {
            get { return this.DataContext as Tbl_Shop; }
            set { this.DataContext = value; }
        }

        //默认查询条件
        Tbl_Shop selectShopInfo = new Tbl_Shop()
        {
            IsValidate = false,
            ShopNo ="请输入车间编号",  //MyStrings.qsrcjbh, 
            ShopName ="请输入车间名称"     //MyStrings.qsrcjmc 
        };

        void InitData()
        {
            tbShopNo.Foreground = tbShopName.Foreground = tbShopName.Foreground = new SolidColorBrush(Colors.Gray);

            tbShopNo.SetBinding(TextBox.TextProperty, new Binding("ShopNo") { Mode = BindingMode.TwoWay });
            tbShopNo.Tag = selectShopInfo.ShopNo;
            tbShopName.SetBinding(TextBox.TextProperty, new Binding("ShopName") { Mode = BindingMode.TwoWay });
            tbShopName.Tag = selectShopInfo.ShopName;

            CuTbl_Shop = new Tbl_Shop()
            {
                IsValidate = false,
                ShopNo = selectShopInfo.ShopNo,
                ShopName = selectShopInfo.ShopName
            };
        }

        void InitEvent()
        {
            btnNew.Click += new RoutedEventHandler(btnNew_Click);
           btnSelect.Click+=new RoutedEventHandler(btnSelect_Click);
            dataClient.GetShopListByConditionCompleted += new EventHandler<DataServiceReference.GetShopListByConditionCompletedEventArgs>(dataClient_GetShopListByConditionCompleted);
            dataClient.GetShopListByConditionCountCompleted += new EventHandler<DataServiceReference.GetShopListByConditionCountCompletedEventArgs>(dataClient_GetShopListByConditionCountCompleted);
            dataClient.DeleteCuShopCompleted += new EventHandler<DataServiceReference.DeleteCuShopCompletedEventArgs>(dataClient_DeleteCuShopCompleted);
            vehiclePageC.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPage_PageIndexChanged);
        }

        #region 查询与分页

        //数据库查询条件对象
        Tbl_Shop selecV = new Tbl_Shop();
        void QueryData()
        {
            selecV = new Tbl_Shop()
            {
                IsValidate = false,
                ShopNo = CuTbl_Shop.ShopNo == selectShopInfo.ShopNo ? "" : CuTbl_Shop.ShopNo,
                ShopName = CuTbl_Shop.ShopName == selectShopInfo.ShopName ? "" : CuTbl_Shop.ShopName
            };
            dataClient.GetShopListByConditionCountAsync(selecV);
        }

        void dataClient_GetShopListByConditionCountCompleted(object sender, DataServiceReference.GetShopListByConditionCountCompletedEventArgs e)
        {
            vehiclePageC.LoadPages(e.Result, pageSize);
        }

        void dpPage_PageIndexChanged(object sender, EventArgs e)
        {
            currentPage = vehiclePageC.PageIndex + 1;
            dataClient.GetShopListByConditionAsync(selecV, pageSize, currentPage);
        }

        void dataClient_GetShopListByConditionCompleted(object sender, DataServiceReference.GetShopListByConditionCompletedEventArgs e)
        {
            gdList.ItemsSource = e.Result;
        }
        #endregion

        void dataClient_DeleteCuShopCompleted(object sender, DataServiceReference.DeleteCuShopCompletedEventArgs e)
        {
            if (e.Result)
                QueryData();
        }

        void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }

        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            var cupage = new NewShopWindow(new Tbl_Shop());
            cupage.Show();
            cupage.Closed += new EventHandler(cupage_Closed);
        }

        void cupage_Closed(object sender, EventArgs e)
        {
            if ((sender as ChildWindow).DialogResult == true)
                QueryData();
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_Shop;
            var cupage = new NewShopWindow(cuObj);
            cupage.Show();
            cupage.Closed += new EventHandler(cupage_Closed);
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyscm, MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_Shop;
            dataClient.DeleteCuShopAsync(cuObj.Id);
        }
    }
}
